package com.example.controller;

import com.example.entity.Menu;
import com.example.entity.User;
import com.example.service.UserService;
import com.example.util.JwtUtils;
import com.example.util.ResultCodeUtil;
import com.example.util.ResultUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : chenjianhua
 * @date : 17:04 2021/2/23
 */

@RestController
@CrossOrigin
public class UserController extends BaseController{

    @Autowired
    private UserService userService;

    @Autowired
    private JwtUtils jwtUtils;

    @PostMapping("/regist")
    public ResultUtil regist(@RequestBody User user){
        try {
            userService.regist(user);
            System.out.println(user);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultUtil(ResultCodeUtil.FAIL,e.getMessage());
        }
        return new ResultUtil(ResultCodeUtil.SUCCESS);
    }

    @PostMapping("/login")
    public ResultUtil login(@RequestBody User user) {
        System.out.println(user);
        HashMap<String, Object> map = new HashMap<>();
        try {
            User res = userService.login(user);
            map.put("username",res.getUsername());
            map.put("password",res.getPassword());
            map.put("sex",res.getSex());
            map.put("tel",res.getTelnumber());
            map.put("userid",res.getUserid());
            map.put("roleid",res.getRoleid());
            String token = jwtUtils.createJwt(map);
            System.out.println(token);
            System.out.println("=============");
            Claims claims = jwtUtils.parseJwt(token);
            System.out.println(claims);

            System.out.println("=============");
            return new ResultUtil(ResultCodeUtil.SUCCESS,token);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultUtil(ResultCodeUtil.USENAMEORPASSWORDERROR, e.getMessage());
        }
    }




    @PostMapping("/qryInfo")
    public ResultUtil qryInfo(){
        Map map = null;
        try {
            map = userService.qryInfo(super.userid);
//            System.out.println(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResultUtil(ResultCodeUtil.SUCCESS,map);
    }


    @PostMapping("/updatePassword")
    public ResultUtil updatePassword(@RequestBody Map<String ,String > map){
        String oldPwd = map.get("oldPwd");
        String newPwd = map.get("newPwd");
//        System.out.println(oldPwd);
//        System.out.println(newPwd);
        Map res = map;
        System.out.println(res);
        try {
            if (!oldPwd.equals(super.password)){
                return new ResultUtil(ResultCodeUtil.OLDPWD_ERROR);
            }
            userService.updatePassword(newPwd,super.userid);
            return new ResultUtil(ResultCodeUtil.UPDATEPWD_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResultUtil(ResultCodeUtil.UPDATEPWD_SUCCESS);
    }




    @PostMapping("/deleteUserById")
    public ResultUtil deleteUserById(@RequestBody Map<String, Integer> map) {
        Integer userid = map.get("userid");
        try {
            System.out.println("删除用户啦。。。。。。。"+map);
            userService.deleteUserById(userid);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultUtil(ResultCodeUtil.FAIL);
        }
        return new ResultUtil(ResultCodeUtil.SUCCESS);
    }



    @PostMapping("/findAllUser")
    public ResultUtil findAllUser(){
        List<User> userList = null;
        try {
            userList = userService.findAllUser();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResultUtil(ResultCodeUtil.SUCCESS,userList);
    }




}
